package com.soupinabox;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * ConnectPanel.java
 * <p/>
 * Author:  Taw
 * Date:    4/21/14
 */
public class ConnectPanel extends JPanel {

    private JTextField nickText;
    private JPasswordField passText;
    private JTextField passVisibleText;
    private JCheckBox showPassCheck;

    public ConnectPanel(){
        super();

        JLabel lab;
        setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.NONE;
        c.anchor = GridBagConstraints.LINE_START;

        lab = new JLabel("Twitch Username: ");
        c.weightx = 0;
        c.gridx = 0;
        c.gridy = 0;
        this.add(lab, c);

        lab = new JLabel("Twitch oAuth Code: ");
        lab.setMinimumSize(lab.getPreferredSize());
        c.gridx = 0;
        c.gridy = 1;
        this.add(lab, c);

        nickText = new JTextField(20);
        nickText.setMinimumSize(nickText.getPreferredSize());
        nickText.addKeyListener(enterListener);
        c.weightx = 1;
        c.gridx = 1;
        c.gridy = 0;
        this.add(nickText, c);

        passText = new JPasswordField(30);
        passText.addKeyListener(enterListener);
        passText.setVisible(true);
        passVisibleText = new JTextField(30);
        passVisibleText.addKeyListener(enterListener);
        passVisibleText.setVisible(false);
        passText.setMinimumSize(passText.getPreferredSize());
        passVisibleText.setMinimumSize(passVisibleText.getPreferredSize());
        c.gridx = 1;
        c.gridy = 1;
        this.add(passText, c);

        showPassCheck = new JCheckBox("Show oAuth Code",false);
        showPassCheck.setActionCommand("check");
        showPassCheck.addActionListener(actionListener);
        c.gridx = 1;
        c.gridy = 2;
        this.add(showPassCheck, c);

        JButton btn = new JButton("Log In");
        btn.setActionCommand("submit");
        btn.addActionListener(actionListener);
        c.gridx = 0;
        c.gridy = 3;
        c.gridwidth=2;
        c.fill = GridBagConstraints.NONE;
        c.anchor = GridBagConstraints.CENTER;
        this.add(btn, c);
    }

    public void submit(){

    }

    public void updatePassText(){
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.LINE_START;
        c.gridx = 1;
        c.gridy = 1;
        c.weightx = 1;
        if (showPassCheck.isSelected()) {
            this.remove(passText);
            this.add(passVisibleText, c);
        } else {
            this.remove(passVisibleText);
            this.add(passText, c);
        }
        this.invalidate();
    }

    private KeyListener enterListener = new KeyAdapter(){
        public void keyPressed(KeyEvent e){
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                submit();
            }
        }
    };

    private ActionListener actionListener = new ActionListener(){
        public void actionPerformed(ActionEvent e){
            if (e.getActionCommand().equals("submit")) {
                submit();
            } else if(e.getActionCommand().equals("check")) {
                updatePassText();
            }
        }
    };

}